//员工列表
//获取列表
obtain()
let listDataS = [];

function obtain() {
  $.ajax({
    url: 'http://localhost:8089/api/shop/category/list',
    type: 'get',
    data: {
    },
    success: function (data) {
      let listData = data.resultInfo.list;
      for (let i = 0; i < listData.length; i++) {
        listDataS.push(listData[i])
      }
      let set = '';
      set = `
              <tr>
                  <th style="width: 60px;"><input type="checkbox" class="checkbox-all">全选</th>
                  <th style="width: 80px;">商品编号</th>
                  <th>商品姓名</th>
                  <th colspan="2" style="width: 200px;">操作</th>
              </tr>
      `
      for (let i = 0; i < listData.length; i++) {
        set += `
        <tr>
            <td> <input type="checkbox" class="checkbox-item"></td>
            <td>${i+1}</td>
            <td>${listDataS[i].name}</td>
            <td><a href="#" onclick="deleteData(${i})">删除</a> </td>
            <td><a href="#" onclick="editForm(${i})">编辑</a></td>
        </tr>
        `
      }
      $('tbody').html(set)
      topForm()
    }
  })
}

// 表格头部
function topForm() {
  let foemW = '' + $('tbody').css('width');
  $('.topBtn').attr('width', foemW)
}
// 打开添加表单
function openForm() {
  $('.employeeForm h1 span').text('添加数据')
  $('.employeeForm').css('display', 'block');
  $('#submit').on('click', function () {
    let nameFoem = $('#name').val(); //店铺
    $.ajax({
      url: 'http://localhost:8089/api/shop/category/add',
      type: 'post',
      data: {
        name: nameFoem, //类名
      },
      success: function (data) {
        let result = data.resultCode
        if (result == 1) {
          alert('成功添加！')
        } else {
          alert('添加失败')
        }
      }
    })
    listDataS = []; //清空数据
    obtain()
    closeForm()
  })

}
// 打开编辑表单
function editForm(id) {
  $('.employeeForm h1 span').text('编辑数据')
  $('.employeeForm').css('display', 'block');

  $('#name').val(listDataS[id].name); //类名
  $('#submit').click(function () {
    listDataS = []; //清空数据
    obtain()
    closeForm()
  })

}

// 关闭表单
function closeForm() {
  $('.employeeForm').css('display', 'none');
  $('#submit').off('click')

  $('#shop').val('');
  $('#product').val('');
  $('#price').val('');
  $('#oldprice').val('');
  $('#detail').val('');
  $('#imgBtn').val('');
  $('#img').attr('src', '');

}

// 提交数据
function SubmitData(num) {
  console.log('开始', num)
}
// 删除数据
function deleteData(num) {
  let idData = listDataS[num].id;
  $.ajax({
    url: 'http://localhost:8089/api/shop/category/delete',
    type: 'get',
    data: {
      id: idData,
    },
    success: function (data) {
      let result = data.resultCode;
      if (result == 1) {
        alert('成功！')
        listDataS = []; //清空数据
        obtain()
      } else {
        alert('失败！')
      }
    }
  })
}
